﻿
using Xacml.Core.Interfaces;
using System;
using System.Collections;
using System.Configuration;
using System.Xml;

namespace Xacml.Core.Configuration
{
  public class ConfigurationRoot
  {
    private ArrayList _attributeRepositories = new ArrayList();
    private ArrayList _policyRepositories = new ArrayList();
    private ArrayList _functionRepositories = new ArrayList();
    private ArrayList _dataTypeRepositories = new ArrayList();
    private ArrayList _ruleCombiningRepositories = new ArrayList();
    private ArrayList _policyCombiningRepositories = new ArrayList();

    public static ConfigurationRoot Config
    {
      get
      {
        return (ConfigurationRoot) ConfigurationSettings.GetConfig("Xacml.net");
      }
    }

    public ArrayList AttributeRepositories
    {
      get
      {
        return this._attributeRepositories;
      }
    }

    public ArrayList PolicyRepositories
    {
      get
      {
        return this._policyRepositories;
      }
    }

    public ArrayList FunctionRepositories
    {
      get
      {
        return this._functionRepositories;
      }
    }

    public ArrayList DataTypeRepositories
    {
      get
      {
        return this._dataTypeRepositories;
      }
    }

    public ArrayList PolicyCombiningAlgorithmRepositories
    {
      get
      {
        return this._policyCombiningRepositories;
      }
    }

    public ArrayList RuleCombiningAlgorithmRepositories
    {
      get
      {
        return this._ruleCombiningRepositories;
      }
    }

    public ConfigurationRoot(XmlNode configNode)
    {
      foreach (XmlNode configNode1 in configNode.SelectNodes("./attributeRepositories/repository"))
      {
        AttributeRepositoryConfig repositoryConfig = new AttributeRepositoryConfig(configNode1);
        IAttributeRepository attributeRepository = (IAttributeRepository) Activator.CreateInstance(repositoryConfig.Type);
        attributeRepository.Init(repositoryConfig.XmlNode);
        this._attributeRepositories.Add((object) attributeRepository);
      }
      foreach (XmlNode configNode1 in configNode.SelectNodes("./policyRepositories/repository"))
      {
        PolicyRepositoryConfig repositoryConfig = new PolicyRepositoryConfig(configNode1);
        IPolicyRepository policyRepository = (IPolicyRepository) Activator.CreateInstance(repositoryConfig.Type);
        policyRepository.Init(repositoryConfig.XmlNode);
        this._policyRepositories.Add((object) policyRepository);
      }
      foreach (XmlNode configNode1 in configNode.SelectNodes("./functionRepositories/repository"))
      {
        FunctionRepositoryConfig repositoryConfig = new FunctionRepositoryConfig(configNode1);
        IFunctionRepository functionRepository = (IFunctionRepository) Activator.CreateInstance(repositoryConfig.Type);
        functionRepository.Init(repositoryConfig.XmlNode);
        this._functionRepositories.Add((object) functionRepository);
      }
      foreach (XmlNode configNode1 in configNode.SelectNodes("./dataTypeRepositories/repository"))
      {
        DataTypeRepositoryConfig repositoryConfig = new DataTypeRepositoryConfig(configNode1);
        IDataTypeRepository dataTypeRepository = (IDataTypeRepository) Activator.CreateInstance(repositoryConfig.Type);
        dataTypeRepository.Init(repositoryConfig.XmlNode);
        this._dataTypeRepositories.Add((object) dataTypeRepository);
      }
      foreach (XmlNode configNode1 in configNode.SelectNodes("./ruleCombiningAlgorithmRepositories/repository"))
      {
        RuleCombiningAlgorithmRepository algorithmRepository1 = new RuleCombiningAlgorithmRepository(configNode1);
        IRuleCombiningAlgorithmRepository algorithmRepository2 = (IRuleCombiningAlgorithmRepository) Activator.CreateInstance(algorithmRepository1.Type);
        algorithmRepository2.Init(algorithmRepository1.XmlNode);
        this._ruleCombiningRepositories.Add((object) algorithmRepository2);
      }
      foreach (XmlNode configNode1 in configNode.SelectNodes("./policyCombiningAlgorithmRepositories/repository"))
      {
        PolicyCombiningAlgorithmRepository algorithmRepository1 = new PolicyCombiningAlgorithmRepository(configNode1);
        IPolicyCombiningAlgorithmRepository algorithmRepository2 = (IPolicyCombiningAlgorithmRepository) Activator.CreateInstance(algorithmRepository1.Type);
        algorithmRepository2.Init(algorithmRepository1.XmlNode);
        this._policyCombiningRepositories.Add((object) algorithmRepository2);
      }
      foreach (XmlNode configNode1 in configNode.SelectNodes("./ruleCombiningAlgorithmRepositories/repository"))
        this._ruleCombiningRepositories.Add((object) new RuleCombiningAlgorithmRepository(configNode1));
    }
  }
}
